<?php
/**
 * CrawlConfig.php
 * 描述
 *
 * 作者:  胡毅 (huyi@comsenz.com)
 * 创建时间: 2012-10-13 下午10:51:05
 * 修改记录:
 *
 * $id$
 */

class Flight_Helper_CrawlConfig {

    /**
     * parseConfig
     * 解析配置
     * 
     * @param unknown_type $config
     * @return multitype:boolean unknown |multitype:boolean mixed
     * 返回两个参数,第一个是配置,第二个是是否返回了多个配置
     */
    public static function parseConfig($config) {

        // 处理url,主要是处理{#page}参数
        $url = $config['crawl_conf']['url'];
        $crawlUrl = array();
        $returnConfig = array();
        if (stripos($url, '{#page}') !== FALSE) {
            if($config['crawl_conf']['urlPage']) {
                if(is_array($config['crawl_conf']['urlPage'])) {
                    if(array_key_exists('start', $config['crawl_conf']['urlPage']) && array_key_exists('end', $config['crawl_conf']['urlPage'])) {
                        $urlPageConfig = array();
                        for ($i = intval($config['crawl_conf']['urlPage']['start']); $i<= intval($config['crawl_conf']['urlPage']['end']); $i++) {
                            $urlPageConfig[] = $i;
                        }
                        $config['crawl_conf']['urlPage'] = $urlPageConfig;
                    }
                    foreach ($config['crawl_conf']['urlPage'] as $urlPage) {
                        $newConfig = $config;
                        $newConfig['crawl_conf']['url'] = str_ireplace('{#page}', $urlPage, $url);
                        $returnConfig[] = $newConfig;
                    }
                    return array($returnConfig, true);
                } else {
                    $config['crawl_conf']['url'] = str_ireplace('{#page}', $config['crawl_conf']['urlPage'], $url);
                }
            }
        }
        return array($config, false);
    }
}